POI2013 Colorful Chain

POI2013 Colorful Chain

题目大意:

给出一个序列p,问有多少个子串满足有$a_i$个$b_i$。

$PS.$ 感觉是做过来最简单的一道题,利用Hash随便$O(n)$扫过来即可。

但似乎不用快速读入能AC但只有99分?

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
const int N=1000002,P=233;
ull f[N],res;
char buf[N*3*8],*p;
void Rd(int&res){
for(++p;*p<32;++p);
for(res=0;*p>47;++p)res=res*10+(*p^48);
}
void Init(){
f[0]=1;
for(int i=1;i<N;i++)f[i]=f[i-1]*P;
}
int n,m,num[N],a[N],tot;
int main(){
Init();
p=buf-1;
fread(buf,1,N*3*8,stdin);
Rd(n),Rd(m);
for(int i=1;i<=m;i++){
Rd(num[i]);
tot+=num[i];
}
for(int i=1,p;i<=m;i++){
Rd(p);
res+=f[p]*num[i];
}
int L=1,R=0,ans=0;
ull cur=0;
for(int i=1,p;i<=n;i++){
R++;
Rd(a[i]);
cur+=f[a[i]];
if(R-L+1>tot)cur-=f[a[L++]];
if(cur==res)ans++;
}
printf("%d\n",ans);
return 0;
}
分享到 评论